<?php

/**
 * Gets content objects from the database.
 * Where clause may be specified in tag body. The tables may be refered to by the following aliases:
 * T: Type
 * I: ElementInstance
 * E: Element
 * C: ElementCats (if attribute cats is given)
 */
m2import('phpinchains/psp/Tag.php');

class cms_getobjectsHandler extends phpinchains_psp_Tag
{

    /**
     * Is a body tag (if false, body is ignored)
     */
    var $tagType = PSP_BODY_TAG;
    /**
     * Tag name in form namespace:tagname
     */
    var $tagName   = 'cms:getobjects';
	var $requiredAttributes = array(
		'var' => '[\w\d_]+',
		'type' => '\w+'
	);
	var $attributes = array(
		'var',          // variable name to store the data
		'varNumResults',// variable name for number of results
		'type',         // object type
		'orderby',      // comma separated list of database fields (use prefixes: D=cmsDoc, I=cmsElementInstance, T=Type Table, E=cmsElement)
		'offset',       // offset of frist dataset to retrieve
		'limit',        // number of datasets to retrieve
                'lang',         // lang_id
                'cats',         // comma separated list of object category ids
                'showquery',
                'site'          // site_id
	);
    
    function doStartTag()
    {
        // initializes $this->buffer and pushes the listener on the stack of the 
        // main output writer
        return PSP_EVAL_BODY_BUFFERED;
    }

    function doEndTag()
    {
        $ctx = base_ApplicationContext::getInstance();
        if ($ctx->cfg('UseNewTPLEngine')) {
            $this->newEngine($ctx);
        } else {
            $this->oldEngine($ctx);
        }
       	return PSP_SKIP_BODY;
    }

    protected function oldEngine($ctx) {
        $lang = $this->getAttribute('lang');
        if (!$lang)
            $lang = ($GLOBALS['lang_id']?$GLOBALS['lang_id']:$GLOBALS['lang']);
        $catsArr = array();
        if ($cats = $this->getAttribute('cats'))
        {
                $catsArr = preg_split('/\s*,\s*/', trim($cats));
        }

        $showquery = ($this->getAttribute('showquery')=='true')?true:false;
        $res = $GLOBALS['dba']->selectContentObjects($this->attrs['type'], trim($this->body->getBuffer()), $lang, $this->getAttribute('orderby'), $this->getAttribute('offset'), $this->getAttribute('limit'), $catsArr, $this->getAttribute('site'), $this->getAttribute('doc'), $showquery);
        $this->actionform->set($this->attrs['var'], $res['data']);
        if ($this->getAttribute('varNumResults'))
                $this->actionform->set($this->getAttribute('varNumResults'), $res['count']);
    }

    protected function newEngine(base_ApplicationContext $ctx) {
        $dctx = $ctx->getDocumentContext();
        $dba = $ctx->getAccessFactory()->getInstanceOf(base_AccessFactory::$CMS);
        $lang = $this->getAttribute('lang');
        if (!$lang)
            $lang = ($dctx->getLanguage('id'));
        $catsArr = array();
        if ($cats = $this->getAttribute('cats'))
        {
            $catsArr = preg_split('/\s*,\s*/', trim($cats));
            if (!function_exists('cast_array_int')) {
                function cast_array_int ($a) {
                    return (int) trim($a);
                }
            }
            $catsArr = array_map('cast_array_int', $catsArr);
        }

        $showquery = ($this->getAttribute('showquery')=='true')?true:false;
        if ($dctx->isLive()) {
            $res = $dba->selectPubContentObjects($this->attrs['type'], trim($this->body->getBuffer()), $lang, $this->getAttribute('orderby'), $this->getAttribute('offset'), $this->getAttribute('limit'), $catsArr, $this->getAttribute('site'), $this->getAttribute('doc'), $showquery);
        } else {
            $res = $dba->selectContentObjects($this->attrs['type'], trim($this->body->getBuffer()), $lang, $this->getAttribute('orderby'), $this->getAttribute('offset'), $this->getAttribute('limit'), $catsArr, $this->getAttribute('site'), $this->getAttribute('doc'), $showquery);
        }
        $this->actionform->set($this->attrs['var'], $res['data']);
        if ($this->getAttribute('varNumResults'))
                $this->actionform->set($this->getAttribute('varNumResults'), $res['count']);
    }

}

?>
